// 音乐音效管理

let AudioMusic = function() {
    let that = {};

     // 播放音频
     // audio               背景音乐的MP3
     // loop                是否循环播放
    that.playYinLiang = function (audio, loop) {
        this.init();
        if (this.yinLiang <= 0) {
            return -1;
        }
        var audioID = cc.audioEngine.play(audio, loop, this.yinLiang);
        return audioID;
    };

    // 播放背景音乐
    // audio               背景音乐的MP3
    that.playerBgMusic = function (audio) {
        if (this.bgId != null) {
            cc.audioEngine.stop(this.bgId);
            this.bgId = null;
        }
        this.bgId = this.playYinLiang(audio, true);
    };

    // 停止背景音乐
    that.stopBgMusic = function () {
        if (this.bgId != null) {
            cc.audioEngine.stop(this.bgId);
        }
    };

    // 音乐音量大小设置
    // yinLiang             音量
    that.setYinLiang = function (yinLiang) {
        cc.sys.localStorage.setItem('yinLiang', yinLiang);
        this.yinLiang = yinLiang;
        if (this.bgId != null) {
            cc.audioEngine.setVolume(this.bgId, this.yinLiang);
        }
    };
    
    // 音效音量大小设置
    // yinXiao              音量
    that.setYinXiao = function (yinXiao) {
        cc.sys.localStorage.setItem('yinXiao', yinXiao);
        this.yinXiao = yinXiao;
    };

    // 获取音乐音量大小
    that.getYinLiang = function () {
        this.init();
        return this.yinLiang;
    };

    // 获得音效音量大小
    that.getYinXiao = function () {
        this.init();
        return this.yinXiao;
    };

    that.init = function () {
        if (this.yinLiang != null && this.yinXiao != null) {
            return;
        }
        var yinLiangS = cc.sys.localStorage.getItem('yinLiang');
        var yinLiang = 1;
        if (yinLiangS != null) {
            yinLiang = parseFloat(yinLiangS);
        }
        else {
            this.setYinLiang(1);
        }
        var yinXiaoS = cc.sys.localStorage.getItem('yinXiao');
        var yinXiao = 1;
        if (yinXiaoS != null) {
            yinXiao = parseFloat(yinXiaoS);
        }
        else {
            this.setYinXiao(yinXiao);
        }
        this.yinLiang = yinLiang;
        this.yinXiao = yinXiao;
    };

    // 播放音效
    // audio                音效的MP3
    that.playYinXiao = function (audio) {
        this.init();
        if (this.yinXiao <= 0) {
            return -1;
        }
        var audioID = cc.audioEngine.play(audio, false, this.yinXiao);
        return audioID;
    };

    return that;
};
export default AudioMusic;